<template>
  <el-card class="change-password-container">
    <template #header>
      <div class="header">
        <h3>修改密码</h3>
      </div>
    </template>
    <el-form :model="form" :rules="rules" ref="formRef" label-width="100px" class="demo-ruleForm">
      <el-form-item label="旧密码" prop="oldPassword">
        <el-input type="password" v-model="form.oldPassword" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="newPassword">
        <el-input type="password" v-model="form.newPassword" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="确认新密码" prop="confirmPassword">
        <el-input type="password" v-model="form.confirmPassword" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue';
import { ElMessage, ElNotification } from 'element-plus';
import md5 from 'js-md5'
import { useRouter } from 'vue-router'
import axios from 'axios';

const router = useRouter()
const formRef = ref(null);
const form = reactive({
  oldPassword: '',
  newPassword: '',
  confirmPassword: ''
});

const rules = reactive({
  oldPassword: [{ required: true, message: '请输入旧密码', trigger: 'blur' }],
  newPassword: [{ required: true, message: '请输入新密码', trigger: 'blur' }],
  confirmPassword: [{ required: true, message: '请再次输入新密码', trigger: 'blur' }, { validator: validatePasswordMatch }]
});

function validatePasswordMatch(rule, value, callback) {
  if (form.newPassword !== value) {
    callback(new Error('两次输入的新密码不一致'));
  } else {
    callback();
  }
}

function submitForm() {
  formRef.value.validate(valid => {
    // form.newPassword = md5(form.newPassword )
    // form.oldPassword = md5(form.oldPassword)
    // form.confirmPassword=md5(form.confirmPassword)

    if (valid) {
      // 发送请求到后端修改密码
      axios.post('/users/changePassword', {
        oldPassword: form.oldPassword,
        newPassword: form.newPassword
      })
          .then(response => {
            if (response.code === 200) {
              ElMessage({
                title: '成功',
                message: response.data.message,
                type: 'success',
              });

              router.push({ path: '/login' })

              resetForm();
            } else {
              ElMessage.error('未知错误，请联系管理员');
            }
          })
          .catch(error => {
            console.error('Error:', error.response.data.message);
            ElMessage.error(error.response.data.message);
          });
    } else {
      console.log('验证失败');
      return false;
    }
  });
}

function resetForm() {
  formRef.value.resetFields();
}
</script>

<style scoped>
.change-password-container {
  margin: 50px auto;
  max-width: 400px;
  padding: 20px;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header h3 {
  margin: 0;
}
</style>